package com.active.activedemo.controller;
import com.active.activedemo.model.BaseProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.editor.language.json.converter.BpmnJsonConverter;
import javax.xml.stream.XMLStreamException;
import java.io.*;


public class test {
    public static void main(String[] args) throws JsonProcessingException, UnsupportedEncodingException, XMLStreamException {
        String jsonStr="{\n" +
                "    \"resourceId\": \"5c5040fe-b196-433d-b34e-3d01cd6c85dc\", \n" +
                "    \"properties\": {\n" +
                "        \"process_id\": \"simpleModel\", \n" +
                "        \"name\": \"简单流程\", \n" +
                "        \"documentation\": \"\", \n" +
                "        \"process_author\": \"\", \n" +
                "        \"process_version\": \"\", \n" +
                "        \"process_namespace\": \"http://www.activiti.org/processdef\", \n" +
                "        \"executionlisteners\": \"\", \n" +
                "        \"eventlisteners\": \"\", \n" +
                "        \"signaldefinitions\": \"\", \n" +
                "        \"messagedefinitions\": \"\"\n" +
                "    }, \n" +
                "    \"stencil\": {\n" +
                "        \"id\": \"BPMNDiagram\"\n" +
                "    }, \n" +
                "    \"childShapes\": [\n" +
                "        {\n" +
                "            \"resourceId\": \"startEvent1\", \n" +
                "            \"properties\": {\n" +
                "                \"overrideid\": \"\", \n" +
                "                \"name\": \"\", \n" +
                "                \"documentation\": \"\", \n" +
                "                \"executionlisteners\": \"\", \n" +
                "                \"initiator\": \"\", \n" +
                "                \"formkeydefinition\": \"\", \n" +
                "                \"formreference\": \"\", \n" +
                "                \"formproperties\": \"\"\n" +
                "            }, \n" +
                "            \"stencil\": {\n" +
                "                \"id\": \"StartNoneEvent\"\n" +
                "            }, \n" +
                "            \"childShapes\": [ ], \n" +
                "            \"outgoing\": [\n" +
                "                {\n" +
                "                    \"resourceId\": \"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\"\n" +
                "                }\n" +
                "            ], \n" +
                "            \"bounds\": {\n" +
                "                \"lowerRight\": {\n" +
                "                    \"x\": 130, \n" +
                "                    \"y\": 193\n" +
                "                }, \n" +
                "                \"upperLeft\": {\n" +
                "                    \"x\": 100, \n" +
                "                    \"y\": 163\n" +
                "                }\n" +
                "            }, \n" +
                "            \"dockers\": [ ]\n" +
                "        }, \n" +
                "        {\n" +
                "            \"resourceId\": \"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\", \n" +
                "            \"properties\": {\n" +
                "                \"overrideid\": \"\", \n" +
                "                \"name\": \"组长审批\", \n" +
                "                \"documentation\": \"\", \n" +
                "                \"asynchronousdefinition\": \"false\", \n" +
                "                \"exclusivedefinition\": \"false\", \n" +
                "                \"executionlisteners\": \"\", \n" +
                "                \"multiinstance_type\": \"None\", \n" +
                "                \"multiinstance_cardinality\": \"\", \n" +
                "                \"multiinstance_collection\": \"\", \n" +
                "                \"multiinstance_variable\": \"\", \n" +
                "                \"multiinstance_condition\": \"\", \n" +
                "                \"isforcompensation\": \"false\", \n" +
                "                \"usertaskassignment\": {\n" +
                "                    \"assignment\": {\n" +
                "                        \"type\": \"idm\", \n" +
                "                        \"idm\": {\n" +
                "                            \"type\": \"users\", \n" +
                "                            \"candidateUsers\": [\n" +
                "                                {\n" +
                "                                    \"id\": \"test\", \n" +
                "                                    \"firstName\": \"test\", \n" +
                "                                    \"lastName\": null, \n" +
                "                                    \"email\": null, \n" +
                "                                    \"fullName\": \"test \", \n" +
                "                                    \"groups\": [ ], \n" +
                "                                    \"$$hashKey\": \"object:6687\"\n" +
                "                                }\n" +
                "                            ]\n" +
                "                        }\n" +
                "                    }\n" +
                "                }, \n" +
                "                \"formkeydefinition\": \"\", \n" +
                "                \"formreference\": \"\", \n" +
                "                \"duedatedefinition\": \"\", \n" +
                "                \"prioritydefinition\": \"\", \n" +
                "                \"formproperties\": \"\", \n" +
                "                \"tasklisteners\": \"\"\n" +
                "            }, \n" +
                "            \"stencil\": {\n" +
                "                \"id\": \"UserTask\"\n" +
                "            }, \n" +
                "            \"childShapes\": [ ], \n" +
                "            \"outgoing\": [\n" +
                "                {\n" +
                "                    \"resourceId\": \"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\"\n" +
                "                }\n" +
                "            ], \n" +
                "            \"bounds\": {\n" +
                "                \"lowerRight\": {\n" +
                "                    \"x\": 332, \n" +
                "                    \"y\": 216\n" +
                "                }, \n" +
                "                \"upperLeft\": {\n" +
                "                    \"x\": 232, \n" +
                "                    \"y\": 136\n" +
                "                }\n" +
                "            }, \n" +
                "            \"dockers\": [ ]\n" +
                "        }, \n" +
                "        {\n" +
                "            \"resourceId\": \"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\", \n" +
                "            \"properties\": {\n" +
                "                \"overrideid\": \"\", \n" +
                "                \"name\": \"\", \n" +
                "                \"documentation\": \"\", \n" +
                "                \"executionlisteners\": \"\"\n" +
                "            }, \n" +
                "            \"stencil\": {\n" +
                "                \"id\": \"EndNoneEvent\"\n" +
                "            }, \n" +
                "            \"childShapes\": [ ], \n" +
                "            \"outgoing\": [ ], \n" +
                "            \"bounds\": {\n" +
                "                \"lowerRight\": {\n" +
                "                    \"x\": 493, \n" +
                "                    \"y\": 193\n" +
                "                }, \n" +
                "                \"upperLeft\": {\n" +
                "                    \"x\": 465, \n" +
                "                    \"y\": 165\n" +
                "                }\n" +
                "            }, \n" +
                "            \"dockers\": [ ]\n" +
                "        }, \n" +
                "        {\n" +
                "            \"resourceId\": \"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\", \n" +
                "            \"properties\": {\n" +
                "                \"overrideid\": \"\", \n" +
                "                \"name\": \"\", \n" +
                "                \"documentation\": \"\", \n" +
                "                \"conditionsequenceflow\": \"\", \n" +
                "                \"executionlisteners\": \"\", \n" +
                "                \"defaultflow\": \"false\"\n" +
                "            }, \n" +
                "            \"stencil\": {\n" +
                "                \"id\": \"SequenceFlow\"\n" +
                "            }, \n" +
                "            \"childShapes\": [ ], \n" +
                "            \"outgoing\": [\n" +
                "                {\n" +
                "                    \"resourceId\": \"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\"\n" +
                "                }\n" +
                "            ], \n" +
                "            \"bounds\": {\n" +
                "                \"lowerRight\": {\n" +
                "                    \"x\": 231.4219467051485, \n" +
                "                    \"y\": 177.8161498108401\n" +
                "                }, \n" +
                "                \"upperLeft\": {\n" +
                "                    \"x\": 130.3514907948515, \n" +
                "                    \"y\": 176.6057251891599\n" +
                "                }\n" +
                "            }, \n" +
                "            \"dockers\": [\n" +
                "                {\n" +
                "                    \"x\": 15, \n" +
                "                    \"y\": 15\n" +
                "                }, \n" +
                "                {\n" +
                "                    \"x\": 50, \n" +
                "                    \"y\": 40\n" +
                "                }\n" +
                "            ], \n" +
                "            \"target\": {\n" +
                "                \"resourceId\": \"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\"\n" +
                "            }\n" +
                "        }, \n" +
                "        {\n" +
                "            \"resourceId\": \"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\", \n" +
                "            \"properties\": {\n" +
                "                \"overrideid\": \"\", \n" +
                "                \"name\": \"\", \n" +
                "                \"documentation\": \"\", \n" +
                "                \"conditionsequenceflow\": \"\", \n" +
                "                \"executionlisteners\": \"\", \n" +
                "                \"defaultflow\": \"false\"\n" +
                "            }, \n" +
                "            \"stencil\": {\n" +
                "                \"id\": \"SequenceFlow\"\n" +
                "            }, \n" +
                "            \"childShapes\": [ ], \n" +
                "            \"outgoing\": [\n" +
                "                {\n" +
                "                    \"resourceId\": \"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\"\n" +
                "                }\n" +
                "            ], \n" +
                "            \"bounds\": {\n" +
                "                \"lowerRight\": {\n" +
                "                    \"x\": 464.14855343232165, \n" +
                "                    \"y\": 178.7738358390709\n" +
                "                }, \n" +
                "                \"upperLeft\": {\n" +
                "                    \"x\": 332.24988406767835, \n" +
                "                    \"y\": 176.7652266609291\n" +
                "                }\n" +
                "            }, \n" +
                "            \"dockers\": [\n" +
                "                {\n" +
                "                    \"x\": 50, \n" +
                "                    \"y\": 40\n" +
                "                }, \n" +
                "                {\n" +
                "                    \"x\": 14, \n" +
                "                    \"y\": 14\n" +
                "                }\n" +
                "            ], \n" +
                "            \"target\": {\n" +
                "                \"resourceId\": \"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\"\n" +
                "            }\n" +
                "        }\n" +
                "    ], \n" +
                "    \"bounds\": {\n" +
                "        \"lowerRight\": {\n" +
                "            \"x\": 1200, \n" +
                "            \"y\": 1050\n" +
                "        }, \n" +
                "        \"upperLeft\": {\n" +
                "            \"x\": 0, \n" +
                "            \"y\": 0\n" +
                "        }\n" +
                "    }, \n" +
                "    \"stencilset\": {\n" +
                "        \"url\": \"stencilsets/bpmn2.0/bpmn2.0.json\", \n" +
                "        \"namespace\": \"http://b3mn.org/stencilset/bpmn2.0#\"\n" +
                "    }, \n" +
                "    \"ssextensions\": [ ]\n" +
                "}";

        //json转换成xml
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(jsonStr);// readTree(这里面是json)
        System.out.println(modelNode);
        BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);// 转model
        BpmnXMLConverter bpmnXMLConverter=new BpmnXMLConverter();
        byte[] convertToXML = bpmnXMLConverter.convertToXML(model);
        String bytes=new String(convertToXML);
        System.out.println(bytes);

        String str="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<definitions xmlns=\"http://www.omg.org/spec/BPMN/20100524/MODEL\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:activiti=\"http://activiti.org/bpmn\" xmlns:bpmndi=\"http://www.omg.org/spec/BPMN/20100524/DI\" xmlns:omgdc=\"http://www.omg.org/spec/DD/20100524/DC\" xmlns:omgdi=\"http://www.omg.org/spec/DD/20100524/DI\" typeLanguage=\"http://www.w3.org/2001/XMLSchema\" expressionLanguage=\"http://www.w3.org/1999/XPath\" targetNamespace=\"http://www.activiti.org/processdef\">\n" +
                "  <process id=\"simpleModel\" name=\"组长\" isExecutable=\"true\">\n" +
                "    <startEvent id=\"startEvent1\"></startEvent>\n" +
                "    <userTask id=\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\" name=\"张三\" activiti:candidateUsers=\"test\">\n" +
                "      <extensionElements>\n" +
                "        <modeler:user-info-firstname-test xmlns:modeler=\"http://activiti.com/modeler\"><![CDATA[test]]></modeler:user-info-firstname-test>\n" +
                "        <modeler:activiti-idm-candidate-user xmlns:modeler=\"http://activiti.com/modeler\"><![CDATA[true]]></modeler:activiti-idm-candidate-user>\n" +
                "        <modeler:initiator-can-complete xmlns:modeler=\"http://activiti.com/modeler\"><![CDATA[false]]></modeler:initiator-can-complete>\n" +
                "      </extensionElements>\n" +
                "    </userTask>\n" +
                "    <endEvent id=\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\"></endEvent>\n" +
                "    <sequenceFlow id=\"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\" sourceRef=\"startEvent1\" targetRef=\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\"></sequenceFlow>\n" +
                "    <sequenceFlow id=\"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\" sourceRef=\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\" targetRef=\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\"></sequenceFlow>\n" +
                "  </process>\n" +
                "  <bpmndi:BPMNDiagram id=\"BPMNDiagram_simpleModel\">\n" +
                "    <bpmndi:BPMNPlane bpmnElement=\"simpleModel\" id=\"BPMNPlane_simpleModel\">\n" +
                "      <bpmndi:BPMNShape bpmnElement=\"startEvent1\" id=\"BPMNShape_startEvent1\">\n" +
                "        <omgdc:Bounds height=\"30.0\" width=\"30.0\" x=\"100.0\" y=\"163.0\"></omgdc:Bounds>\n" +
                "      </bpmndi:BPMNShape>\n" +
                "      <bpmndi:BPMNShape bpmnElement=\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\" id=\"BPMNShape_sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\">\n" +
                "        <omgdc:Bounds height=\"80.0\" width=\"100.0\" x=\"232.0\" y=\"136.0\"></omgdc:Bounds>\n" +
                "      </bpmndi:BPMNShape>\n" +
                "      <bpmndi:BPMNShape bpmnElement=\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\" id=\"BPMNShape_sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\">\n" +
                "        <omgdc:Bounds height=\"28.0\" width=\"28.0\" x=\"465.0\" y=\"165.0\"></omgdc:Bounds>\n" +
                "      </bpmndi:BPMNShape>\n" +
                "      <bpmndi:BPMNEdge bpmnElement=\"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\" id=\"BPMNEdge_sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\">\n" +
                "        <omgdi:waypoint x=\"129.99892442277218\" y=\"177.82037216260153\"></omgdi:waypoint>\n" +
                "        <omgdi:waypoint x=\"232.0\" y=\"176.5988023952096\"></omgdi:waypoint>\n" +
                "      </bpmndi:BPMNEdge>\n" +
                "      <bpmndi:BPMNEdge bpmnElement=\"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\" id=\"BPMNEdge_sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\">\n" +
                "        <omgdi:waypoint x=\"332.0\" y=\"176.76142131979697\"></omgdi:waypoint>\n" +
                "        <omgdi:waypoint x=\"465.0016230525035\" y=\"178.78682674699243\"></omgdi:waypoint>\n" +
                "      </bpmndi:BPMNEdge>\n" +
                "    </bpmndi:BPMNPlane>\n" +
                "  </bpmndi:BPMNDiagram>\n" +
                "</definitions>\n";


       /* InputStream bpmnStream = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader in = new InputStreamReader(bpmnStream, "UTF-8");
        XMLStreamReader xtr = xif.createXMLStreamReader(in);
        // 然后转为bpmnModel
        BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
        // bpmnModel转json
        BpmnJsonConverter converter = new BpmnJsonConverter();
        com.fasterxml.jackson.databind.node.ObjectNode editorJsonNode = converter.convertToJson(bpmnModel);
        System.out.println(editorJsonNode);*/


       /* String strs="{\"bounds\":{\"lowerRight\":{\"x\":1485.0,\"y\":700.0},\"upperLeft\":{\"x\":0.0,\"y\":0.0}},\"resourceId\":\"canvas\",\"stencil\":{\"id\":\"BPMNDiagram\"},\"stencilset\":{\"namespace\":\"http://b3mn.org/stencilset/bpmn2.0#\",\"url\":\"../editor/stencilsets/bpmn2.0/bpmn2.0.json\"},\"properties\":{\"process_id\":\"simpleModel\",\"name\":\"组长\",\"process_namespace\":\"http://www.activiti.org/processdef\",\"messages\":[],\"executionlisteners\":{\"executionListeners\":[]},\"eventlisteners\":{\"eventListeners\":[]},\"signaldefinitions\":[],\"messagedefinitions\":[]},\"childShapes\":[{\"bounds\":{\"lowerRight\":{\"x\":130.0,\"y\":193.0},\"upperLeft\":{\"x\":100.0,\"y\":163.0}},\"resourceId\":\"startEvent1\",\"childShapes\":[],\"stencil\":{\"id\":\"StartNoneEvent\"},\"properties\":{\"overrideid\":\"startEvent1\",\"executionlisteners\":{\"executionListeners\":[]}},\"outgoing\":[{\"resourceId\":\"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\"}]},{\"bounds\":{\"lowerRight\":{\"x\":332.0,\"y\":216.0},\"upperLeft\":{\"x\":232.0,\"y\":136.0}},\"resourceId\":\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\",\"childShapes\":[],\"stencil\":{\"id\":\"UserTask\"},\"properties\":{\"overrideid\":\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\",\"name\":\"张三\",\"usertaskassignment\":{\"assignment\":{\"type\":\"idm\",\"idm\":{\"candidateUsers\":[{\"id\":\"test\",\"firstName\":\"test\"}],\"type\":\"users\"},\"initiatorCanCompleteTask\":false}},\"asynchronousdefinition\":false,\"exclusivedefinition\":true,\"tasklisteners\":{\"taskListeners\":[]},\"executionlisteners\":{\"executionListeners\":[]}},\"outgoing\":[{\"resourceId\":\"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\"}]},{\"bounds\":{\"lowerRight\":{\"x\":493.0,\"y\":193.0},\"upperLeft\":{\"x\":465.0,\"y\":165.0}},\"resourceId\":\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\",\"childShapes\":[],\"stencil\":{\"id\":\"EndNoneEvent\"},\"properties\":{\"overrideid\":\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\",\"executionlisteners\":{\"executionListeners\":[]}},\"outgoing\":[]},{\"bounds\":{\"lowerRight\":{\"x\":172.0,\"y\":212.0},\"upperLeft\":{\"x\":128.0,\"y\":212.0}},\"resourceId\":\"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\",\"childShapes\":[],\"stencil\":{\"id\":\"SequenceFlow\"},\"dockers\":[{\"x\":15.0,\"y\":15.0},{\"x\":50.0,\"y\":40.0}],\"outgoing\":[{\"resourceId\":\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\"}],\"target\":{\"resourceId\":\"sid-584C69B4-A76F-4396-8FE1-1FC77C0385CC\"},\"properties\":{\"overrideid\":\"sid-1007C45C-AB28-4EA8-87C9-0E35B6FFA141\"}},{\"bounds\":{\"lowerRight\":{\"x\":172.0,\"y\":212.0},\"upperLeft\":{\"x\":128.0,\"y\":212.0}},\"resourceId\":\"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\",\"childShapes\":[],\"stencil\":{\"id\":\"SequenceFlow\"},\"dockers\":[{\"x\":50.0,\"y\":40.0},{\"x\":14.0,\"y\":14.0}],\"outgoing\":[{\"resourceId\":\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\"}],\"target\":{\"resourceId\":\"sid-5D1AABEF-2AB6-46E9-9AD5-0CD36AD6251C\"},\"properties\":{\"overrideid\":\"sid-CD98690F-4EA7-4F5F-9A04-FC604815AD2B\"}}]}";
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(strs);// readTree(这里面是json)
        System.out.println(modelNode);
        BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);// 转model
        BpmnXMLConverter bpmnXMLConverter=new BpmnXMLConverter();
        byte[] convertToXML = bpmnXMLConverter.convertToXML(model);
        String bytes=new String(convertToXML);
        System.out.println(bytes);*/

    }
}
